package com.woniuxy.finance.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.woniuxy.common.util.CheckTokenUtil;
import com.woniuxy.finance.model.entity.Blotter;
import com.woniuxy.finance.model.entity.BusinessCompetitor;
import com.woniuxy.finance.model.entity.Recharge;
import com.woniuxy.finance.service.ExportTableService;
import com.woniuxy.model.entity.ExportTable;
import com.woniuxy.model.entity.SaleMoney;
import com.woniuxy.response.ResponseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/export")
public class ExportController {

    @Resource
    private ExportTableService exportTableService;

    /**
     * 导出客户信息
     * @param list
     * @return
     */
    @PostMapping("/clientExcel")
    public ResponseResult<Boolean> clientExcel(@RequestBody List<BusinessCompetitor> list, HttpServletRequest request) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\java\\project\\four\\excel\\客户信息表.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        //writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
        writer.merge(list.size(), "客户信息表");
        //起别名
        writer.addHeaderAlias("name","客户名称");
        writer.addHeaderAlias("uniformCreditCode","信用代码");
        writer.addHeaderAlias("mark","代号");
        writer.addHeaderAlias("encoding","编码");
        writer.addHeaderAlias("area","区域");
        writer.addHeaderAlias("balance","余额");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("adminName","经办人");
        writer.addHeaderAlias("createTime","创建时间");
        //排除未起别名的字段
        writer.setOnlyAlias(true);
        //一次性写出内容，强制输出标题
        ExcelWriter write = writer.write(list, true);
        System.out.println(write);
        //关闭writer，释放内存
        writer.close();
        if(StrUtil.length(String.valueOf(write)) >0){
            String u = request.getHeader("access_token");
            Map<String, Object> user = CheckTokenUtil.getUser(u);
            String account = (String) user.get("account");
            ExportTable exportTable = new ExportTable();
            exportTable.setAccount(account);//导出人员名称
            exportTable.setExportTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//导出时间
            exportTable.setTableName("客户信息表");
            exportTableService.save(exportTable);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }

    }

    /**
     * 导出供应商信息
     * @param list
     * @return
     */
    @PostMapping("/vendorExcel")
    public ResponseResult<Boolean> vendorExcel(@RequestBody List<BusinessCompetitor> list, HttpServletRequest request) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\java\\project\\four\\excel\\供应商信息表.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        //writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
        writer.merge(list.size(), "供应商信息表");
        //起别名
        writer.addHeaderAlias("name","供应商名称");
        writer.addHeaderAlias("uniformCreditCode","信用代码");
        writer.addHeaderAlias("mark","代号");
        writer.addHeaderAlias("encoding","编码");
        writer.addHeaderAlias("area","区域");
        writer.addHeaderAlias("balance","余额");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("adminName","经办人");
        writer.addHeaderAlias("createTime","创建时间");
        //排除未起别名的字段
        writer.setOnlyAlias(true);
        //一次性写出内容，强制输出标题
        ExcelWriter write = writer.write(list, true);
        //关闭writer，释放内存
        writer.close();
        if(StrUtil.length(String.valueOf(write)) >0){
            String u = request.getHeader("access_token");
            Map<String, Object> user = CheckTokenUtil.getUser(u);
            String account = (String) user.get("account");
            ExportTable exportTable = new ExportTable();
            exportTable.setAccount(account);//导出人员名称
            exportTable.setExportTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//导出时间
            exportTable.setTableName("供应商信息表");
            exportTableService.save(exportTable);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }

    /**
     * 导出账单信息
     * @param list
     * @return
     */
    @PostMapping("/itemExcel")
    public ResponseResult<Boolean> itemExcel(@RequestBody List<SaleMoney> list, HttpServletRequest request) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\java\\project\\four\\excel\\客户账单信息表.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        //writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
        writer.merge(list.size(), "客户账单信息表");
        //起别名
        writer.addHeaderAlias("customer","客户名称");
        writer.addHeaderAlias("sno","账单编号");
        writer.addHeaderAlias("type","账单类型");
        writer.addHeaderAlias("totalPrice","总金额");
        writer.addHeaderAlias("operator","经办人");
        writer.addHeaderAlias("operatorTime","经办时间");
        //排除未起别名的字段
        writer.setOnlyAlias(true);
        //一次性写出内容，强制输出标题
        ExcelWriter write = writer.write(list, true);
        //关闭writer，释放内存
        writer.close();
        if(StrUtil.length(String.valueOf(write)) >0){
            String u = request.getHeader("access_token");
            Map<String, Object> user = CheckTokenUtil.getUser(u);
            String account = (String) user.get("account");
            ExportTable exportTable = new ExportTable();
            exportTable.setAccount(account);//导出人员名称
            exportTable.setExportTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//导出时间
            exportTable.setTableName("客户账单信息表");
            exportTableService.save(exportTable);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 导出充值详情
     */
    @PostMapping("/rechargeExcel")
    public ResponseResult<Boolean> rechargeExcel(@RequestBody List<Recharge> list, HttpServletRequest request) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\java\\project\\four\\excel\\充值明细表.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        //writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
        writer.merge(list.size(), "充值明细表");
        //起别名
        writer.addHeaderAlias("userName","客户名称");
        writer.addHeaderAlias("rechargeNumber","充值编号");
        writer.addHeaderAlias("money","充值金额");
        writer.addHeaderAlias("receipt","凭证存放位置");
        writer.addHeaderAlias("createTime","充值时间");
        writer.addHeaderAlias("adminName","经办人");
        //排除未起别名的字段
        writer.setOnlyAlias(true);
        //一次性写出内容，强制输出标题
        ExcelWriter write = writer.write(list, true);
        //关闭writer，释放内存
        writer.close();
        if(StrUtil.length(String.valueOf(write)) >0){
            String u = request.getHeader("access_token");
            Map<String, Object> user = CheckTokenUtil.getUser(u);
            String account = (String) user.get("account");
            ExportTable exportTable = new ExportTable();
            exportTable.setAccount(account);//导出人员名称
            exportTable.setExportTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//导出时间
            exportTable.setTableName("充值明细表");
            exportTableService.save(exportTable);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
    /**
     * 导出友商流水明细
     */
    @PostMapping("/blotterExcel")
    public ResponseResult<Boolean> blotterExcel(@RequestBody List<Blotter> list, HttpServletRequest request) {
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\java\\project\\four\\excel\\友商流水明细表.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        //writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
        writer.merge(list.size(), "流水明细表");
        //起别名
        writer.addHeaderAlias("name","客户名称");
        writer.addHeaderAlias("sno","账单编号");
        writer.addHeaderAlias("status","费用类型（1：充值 2 定金 3 尾款 4 退款）");
        writer.addHeaderAlias("totalPrice","账单金额");
        writer.addHeaderAlias("adminName","经办人");
        writer.addHeaderAlias("createTime","经办时间");
        //排除未起别名的字段
        writer.setOnlyAlias(true);
        //一次性写出内容，强制输出标题
        ExcelWriter write = writer.write(list, true);
        //关闭writer，释放内存
        writer.close();
        if(StrUtil.length(String.valueOf(write)) >0){
            String u = request.getHeader("access_token");
            Map<String, Object> user = CheckTokenUtil.getUser(u);
            String account = (String) user.get("account");
            ExportTable exportTable = new ExportTable();
            exportTable.setAccount(account);//导出人员名称
            exportTable.setExportTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//导出时间
            exportTable.setTableName("友商流水明细表");
            exportTableService.save(exportTable);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else{
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"fail",false);
        }
    }
}
